package com.why.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.why.hosp.repository.DepartmentRepository;
import com.why.hosp.service.DepartmentService;
import com.why.yygh.model.hosp.Department;
import com.why.yygh.vo.hosp.DepartmentQueryVo;
import com.why.yygh.vo.hosp.DepartmentVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        Department department = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Department.class);
//        查询部门信息：根据医院和部门的编号 必须有医院的编号
        Department targetDepartment = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (null != targetDepartment) {
//copy不为null的值，该方法为自定义方法
            BeanUtils.copyProperties(department, targetDepartment, Department.class);
            departmentRepository.save(targetDepartment);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    @Override
    public Page<Department> selectPage(Integer page, Integer limit, DepartmentQueryVo departmentQueryVo) {
//        创建pageable对象，0
        Pageable pageable = PageRequest.of(page - 1, limit);


//        创建Example对象
//创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        Department department = new Department();

        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        Example<Department> example = Example.of(department, matcher);

        return departmentRepository.findAll(example, pageable);
    }

    /**
     * 先查询 如果不为空再删除
     * @param hoscode
     * @param depcode
     */
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (null != department) {
            departmentRepository.deleteById(department.getId());
        }
    }

    /**
     * 根据父类id查询其所有的科
     *
     * @param hoscode
     * @return
     */
    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {

        Department department = new Department();
        department.setHoscode(hoscode);

        Example<Department> example = Example.of(department);
//        所有的科室信息
        List<Department> departments = departmentRepository.findAll(example);
        Map<String, List<Department>> collect = departments.stream().collect(Collectors.groupingBy(Department::getBigcode));

        List<DepartmentVo> result = new ArrayList<>();
        for (Map.Entry<String, List<Department>> entry : collect.entrySet()) {
//            科室的id
            String departmentId = entry.getKey();
//            所有的科室
            List<Department> departmentList = entry.getValue();

//            封装科室，首先封装主科室，然后遍历集合，将其封装起来。
            if (departmentList.size() > 0) {
                DepartmentVo departmentVo = new DepartmentVo();
                //            这个是主科室 eg：内科,因为每个数据都保存了 bigcode和bigname，因此我们随便取一个拿到它的名字和id就行
                departmentVo.setDepcode(departmentList.get(0).getBigcode());
                departmentVo.setDepname(departmentList.get(0).getBigname());
//            封装小科室
                ArrayList<DepartmentVo> children = new ArrayList<>();
                for (Department department1 : departmentList) {
                    DepartmentVo departmentVo1 = new DepartmentVo();
                    departmentVo1.setDepname(department1.getDepname());
                    departmentVo1.setDepcode(department1.getDepcode());
                    children.add(departmentVo1);
                }
                departmentVo.setChildren(children);
                result.add(departmentVo);
            }



        }

        return result;
    }
    //根据科室编号，和医院编号，查询科室名称
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode,depcode);
    }

}
